home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / Apache 1.0 / src / mod_log_agent.c < prev    next >
Text File  |  1995-12-04  |  6KB  |  187 lines

  1.  
  2. /* ====================================================================
  3.  * Copyright (c) 1995 The Apache Group.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer. 
  11.  *
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in
  14.  *    the documentation and/or other materials provided with the
  15.  *    distribution.
  16.  *
  17.  * 3. All advertising materials mentioning features or use of this
  18.  *    software must display the following acknowledgment:
  19.  *    "This product includes software developed by the Apache Group
  20.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  21.  *
  22.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  23.  *    endorse or promote products derived from this software without
  24.  *    prior written permission.
  25.  *
  26.  * 5. Redistributions of any form whatsoever must retain the following
  27.  *    acknowledgment:
  28.  *    "This product includes software developed by the Apache Group
  29.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  30.  *
  31.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  32.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  35.  * IT'S CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  42.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  43.  * ====================================================================
  44.  *
  45.  * This software consists of voluntary contributions made by many
  46.  * individuals on behalf of the Apache Group and was originally based
  47.  * on public domain software written at the National Center for
  48.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  49.  * For more information on the Apache Group and the Apache HTTP server
  50.  * project, please see <http://www.apache.org/>.
  51.  *
  52.  */
  53.  
  54.  
  55.  
  56. #include "httpd.h"
  57. #include "http_config.h"
  58.  
  59. #define DEFAULT_AGENTLOG "logs/agent_log"
  60.  
  61. module agent_log_module;
  62.  
  63. static int xfer_flags = ( O_WRONLY | O_APPEND | O_CREAT );
  64. static mode_t xfer_mode = ( S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH );
  65.  
  66. typedef struct {
  67.     char *fname;
  68.     int agent_fd;
  69. } agent_log_state;
  70.  
  71. void *make_agent_log_state (pool *p, server_rec *s)
  72. {
  73.     agent_log_state *cls =
  74.       (agent_log_state *)palloc (p, sizeof (agent_log_state));
  75.  
  76.     cls->fname = DEFAULT_AGENTLOG;
  77.     cls->agent_fd = -1;
  78.  
  79.  
  80.     return (void *)cls;
  81. }
  82.  
  83. char *set_agent_log (cmd_parms *parms, void *dummy, char *arg)
  84. {
  85.     agent_log_state *cls = get_module_config (parms->server->module_config,
  86.                            &agent_log_module);
  87.   
  88.     cls->fname = arg;
  89.     return NULL;
  90. }
  91.  
  92. command_rec agent_log_cmds[] = {
  93. { "AgentLog", set_agent_log, NULL, RSRC_CONF, TAKE1,
  94.     "the filename of the agent log" },
  95. { NULL }
  96. };
  97.  
  98. void agent_log_child (void *cmd)
  99. {
  100.     /* Child process code for 'AgentLog "|..."';
  101.      * may want a common framework for this, since I expect it will
  102.      * be common for other foo-loggers to want this sort of thing...
  103.      */
  104.     
  105.     cleanup_for_exec();
  106.     signal (SIGHUP, SIG_IGN);
  107.     execl (SHELL_PATH, SHELL_PATH, "-c", (char *)cmd, NULL);
  108.     exit (1);
  109. }
  110.  
  111. void open_agent_log (server_rec *s, pool *p)
  112. {
  113.     agent_log_state *cls = get_module_config (s->module_config,
  114.                            &agent_log_module);
  115.   
  116.     char *fname = server_root_relative (p, cls->fname);
  117.     
  118.     if (cls->agent_fd > 0) return; /* virtual log shared w/main server */
  119.     
  120.     if (*cls->fname == '|') {
  121.     FILE *dummy;
  122.     
  123.     spawn_child(p, agent_log_child, (void *)(cls->fname+1),
  124.             kill_after_timeout, &dummy, NULL);
  125.  
  126.     if (dummy == NULL) {
  127.         fprintf (stderr, "Couldn't fork child for AgentLog process\n");
  128.         exit (1);
  129.     }
  130.  
  131.     cls->agent_fd = fileno (dummy);
  132.     }
  133.     else if((cls->agent_fd = popenf(p, fname, xfer_flags, xfer_mode)) < 0) {
  134.         fprintf(stderr,"httpd: could not open transfer log file %s.\n", fname);
  135.         perror("open");
  136.         exit(1);
  137.     }
  138. }
  139.  
  140. void init_agent_log (server_rec *s, pool *p)
  141. {
  142.     for (; s; s = s->next) open_agent_log (s, p);
  143. }
  144.  
  145. int agent_log_transaction(request_rec *orig)
  146. {
  147.     agent_log_state *cls = get_module_config (orig->server->module_config,
  148.                            &agent_log_module);
  149.   
  150.     char str[HUGE_STRING_LEN];
  151.     char *agent;
  152.     request_rec *r;
  153.  
  154.     if(cls->agent_fd <0)
  155.       return OK;
  156.  
  157.     for (r = orig; r->next; r = r->next)
  158.         continue;
  159.  
  160.     agent = table_get(orig->headers_in, "User-Agent");
  161.     if(agent != NULL) 
  162.       {
  163.     sprintf(str, "%s\n", agent);
  164.     write(cls->agent_fd, str, strlen(str));
  165.       }
  166.     
  167.     return OK;
  168. }
  169.  
  170. module agent_log_module = {
  171.    STANDARD_MODULE_STUFF,
  172.    init_agent_log,        /* initializer */
  173.    NULL,            /* create per-dir config */
  174.    NULL,            /* merge per-dir config */
  175.    make_agent_log_state,    /* server config */
  176.    NULL,            /* merge server config */
  177.    agent_log_cmds,        /* command table */
  178.    NULL,            /* handlers */
  179.    NULL,            /* filename translation */
  180.    NULL,            /* check_user_id */
  181.    NULL,            /* check auth */
  182.    NULL,            /* check access */
  183.    NULL,            /* type_checker */
  184.    NULL,            /* fixups */
  185.    agent_log_transaction    /* logger */
  186. };
  187.